<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Permission;
use Validator;

class PermissionController extends Controller
{
    //
    protected function index(){
	
		$list =  Permission::orderBy('sort','desc')->orderBy('id','asc')->get()->toarray();
		$level_list = array2level($list);
    	return view('admin/permission/index',['list'=>$level_list]);
    }    

	//添加
    protected function add(){
		if(request()->isMethod('post')){
			$input = request()->only(['name','display_name','description','pid','sort','icon','status']);					
	        $validator = Validator::make($input, [
	            'name' => 'bail|required|unique:permissions',
	            'display_name' => 'bail|required',
	        ]);	
	        if ($validator->fails()) {
	            $message = $validator->errors()->first();
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> $message,
					'url'	=>''
					));
	        }
			$permission = new Permission;
			$permission->name			= $input['name'];
			$permission->display_name	= $input['display_name'];
			$permission->description	= $input['description'];
			$permission->pid			= $input['pid'];
			$permission->sort			= $input['sort'];	
			$permission->icon			= $input['icon'];	
			$permission->status			= $input['status'];	
			if($permission->save()){
				return response()->json(array(
					'code'	=> 1,
					'msg'	=> '添加成功',
					'url'	=>route('admin.permission.index')
					));					
			}else{
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> '添加失败',
					'url'	=> '',
					));
			}		
		}		
		
		$id = request('id', '');
		$list =  Permission::orderBy('sort','desc')->orderBy('id','asc')->get()->toarray();
		$level_list = array2level($list);		
    	return view('admin/permission/add',['id'=>$id,'list'=>$level_list]);
    }
    
	//删除
	protected function del(){
		$id = request('id');
		$permission = Permission::find($id);
		if($permission->delete()){
			return response()->json(array(
				'code'	=> 1,
				'msg'	=> '删除成功',
				'url'	=>''
				));					
		}else{
			return response()->json(array(
				'code'	=> 0,
				'msg'	=> '删除失败',
				'url'	=>''
				));				
		}		
	}
	
	//编辑
	protected function edit(){
		
		if(request()->isMethod('post')){
			$input = request()->only(['id','name','display_name','description','pid','sort','icon','status']);
	        $validator = Validator::make($input, [
	            'name' => 'bail|required|unique:permissions,name,'.$input['id'],
	            'display_name' => 'bail|required',
	        ]);	
	        if ($validator->fails()) {
	            $message = $validator->errors()->first();
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> $message,
					'url'	=>''
					));	
	        }		
			$permission = Permission::find($input['id']);
			$permission->name			= $input['name'];
			$permission->display_name	= $input['display_name'];
			$permission->description	= $input['description'];
			$permission->pid			= $input['pid'];
			$permission->sort			= $input['sort'];	
			$permission->icon			= $input['icon'];				
			$permission->status			= $input['status'];	
			if($permission->save()){
				return response()->json(array(
					'code'	=> 1,
					'msg'	=> '编辑成功',
					'url'	=>route('admin.permission.index')
					));					
			}else{
				return response()->json(array(
					'code'	=> 0,
					'msg'	=> '编辑失败',
					'url'	=> '',
					));
			}			
		}
		
		$id = request('id');
		$res = Permission::find($id);
		$list =  Permission::orderBy('sort','desc')->orderBy('id','asc')->get()->toarray();
		$level_list = array2level($list);			
		return view('admin/permission/edit',['data'=>$res,'list'=>$level_list]);
	}
}
